//
//  UIBarButtonItem+custom.m
//  YDGJ
//
//  Created by ydcq on 16/5/5.
//  Copyright © 2016年 Galaxy360. All rights reserved.
//

#import "UIBarButtonItem+custom.h"
#import <objc/runtime.h>

static void *blockKey = "blockKey";

@implementation UIBarButtonItem (custom)

+ (UIBarButtonItem *)barButtonWithTitle:(NSString *)title titleColor:(UIColor *)color target:(id)target selector:(SEL)selector
{
    color = WhiteColor;
    CGFloat width = [title widthWithFont:FontCustomSize(15) constrainedToHeight:20];
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.frame = CGRectMake(0, 0, width, 40);
    [addBtn setTitle:title forState:UIControlStateNormal];
    addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [addBtn setTitleColor:color forState:UIControlStateNormal];
    [addBtn setTitleColor:[color colorWithAlphaComponent:0.4] forState:UIControlStateHighlighted];
    addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    addBtn.titleLabel.font = FontCustomSize(15);
    addBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    if (target && selector) {
        [addBtn addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    }    
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:addBtn];
    return barButton;
}

+ (UIBarButtonItem *)barButtonWithTitle:(NSString *)title titleColor:(UIColor *)color clickBlock:(dispatch_block_t)blk
{
    color = WhiteColor;
    CGFloat width = [title widthWithFont:FontCustomSize(15) constrainedToHeight:20];
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.frame = CGRectMake(0, 0, width, 40);
    [addBtn setTitle:title forState:UIControlStateNormal];
    addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [addBtn setTitleColor:color forState:UIControlStateNormal];
    [addBtn setTitleColor:[color colorWithAlphaComponent:0.4] forState:UIControlStateHighlighted];
    addBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    addBtn.titleLabel.font = FontCustomSize(15);
    addBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:addBtn];
    [addBtn addTarget:barButton action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    objc_setAssociatedObject(barButton, "", blk, OBJC_ASSOCIATION_COPY_NONATOMIC);
    return barButton;
}

- (void)click
{
    dispatch_block_t blk = objc_getAssociatedObject(self, "");
    if (blk) {
        blk();
    }
}

- (void)setClickBlock:(void (^)())clickBlock
{
    self.target = self;
    self.action = @selector(itemClick);
    objc_setAssociatedObject(self, blockKey, clickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (void (^)())clickBlock
{
    return objc_getAssociatedObject(self, blockKey);
}

- (void)itemClick
{
    if (self.clickBlock) {
        self.clickBlock();
    }
}

- (NSString *)titleName
{
    if ([self.customView isKindOfClass:[UIButton class]]) {
        return ((UIButton *)self.customView).titleLabel.text;
    }
    return nil;
}

- (void)setTitleName:(NSString *)title
{
    if ([self.customView isKindOfClass:[UIButton class]]) {
        [((UIButton *)self.customView) setTitle:title forState:UIControlStateNormal];
    }
}

@end
